package com.xinchao.ck_mips.utils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Base64;

import com.xinchao.ck_mips.App;
import com.xinchao.ck_mips.BuildConfig;
import com.xinchao.ck_mips.constant.CK;

public class ShareUtil {

    private static ShareUtil sShareUtil;
    private SharedPreferences mSharedPreferences;
    private boolean isEncodeKey;//是否加密key

    private ShareUtil() {
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(App.getApp().getApplicationContext());
        isEncodeKey = true;
    }

    public static ShareUtil getInstance() {
        if (sShareUtil == null) {
            synchronized (ShareUtil.class) {
                if (sShareUtil == null) {
                    sShareUtil = new ShareUtil();
                }
            }
        }
        return sShareUtil;
    }
    
    public void setEncodeKey(boolean encodeKey) {
        isEncodeKey = encodeKey;
    }

    public void put(String key, Object value) {
        checkKeyNull(key);
        if (value == null || TextUtils.isEmpty(value.toString()))
            throw new NullPointerException("Value is null");
        if (value instanceof String) {
            mSharedPreferences.edit().putString(encodeKey(key), value.toString()).apply();
        } else if (value instanceof Integer) {
            mSharedPreferences.edit().putInt(encodeKey(key), Integer.parseInt(value.toString())).apply();
        } else if (value instanceof Boolean) {
            mSharedPreferences.edit().putBoolean(encodeKey(key), Boolean.parseBoolean(value.toString())).apply();
        } else if (value instanceof Float) {
            mSharedPreferences.edit().putFloat(encodeKey(key), Float.parseFloat(value.toString())).apply();
        } else if (value instanceof Long) {
            mSharedPreferences.edit().putLong(encodeKey(key), Long.parseLong(value.toString())).apply();
        } else {
            throw new IllegalArgumentException("Value can not save in SharedPreference");
        }
    }

    /**
     * 保存token,同时保存到本地文件中
     *
     * @param token
     */
    public void putToken(String token) {
        checkKeyNull(token);
        if (BuildConfig.DEBUG_LOG) {
            //调试模式
            String key = encodeKey(CK.SP_DEBUG_TOKEN);
            mSharedPreferences.edit().putString(key, token.toString()).apply();
            FileUtils.saveDebugToken(token);
        } else {
            //正式环境
            String key = encodeKey(CK.SP_TOKEN);
            mSharedPreferences.edit().putString(key, token.toString()).apply();
            FileUtils.saveToken(token);
        }

    }

    /**
     * 保存工作日开机时间
     *
     * @param onTime
     */
    public void saveWorkDayOnTime(String onTime) {
        checkKeyNull(onTime);
        mSharedPreferences.edit().putString(encodeKey(CK.WORK_DAY_ON_TIME), onTime).apply();
    }

    /**
     * 保存工作日关机时间
     *
     * @param offTime
     */
    public void saveWorkDayOffTime(String offTime) {
        checkKeyNull(offTime);
        mSharedPreferences.edit().putString(encodeKey(CK.WORK_DAY_OFF_TIME), offTime).apply();
    }

    /**
     * 保存非工作日开机时间
     *
     * @param onTime
     */
    public void saveWeekDayOnTime(String onTime) {
        checkKeyNull(onTime);
        mSharedPreferences.edit().putString(encodeKey(CK.WEEK_DAY_ON_TIME), onTime).apply();
    }

    /**
     * 保存非工作日关机时间
     *
     * @param offTime
     */
    public void saveWeekDayOffTime(String offTime) {
        checkKeyNull(offTime);
        mSharedPreferences.edit().putString(encodeKey(CK.WEEK_DAY_OFF_TIME), offTime).apply();
    }


    /**
     * 获取token
     *
     * @return
     */
    public String getToken() {
        if (BuildConfig.DEBUG_LOG) {
            //测试环境
            if (!TextUtils.isEmpty(mSharedPreferences.getString(isEncodeKey ? encodeKey(CK.SP_DEBUG_TOKEN) : CK.SP_DEBUG_TOKEN, ""))) {
                return mSharedPreferences.getString(isEncodeKey ? encodeKey(CK.SP_DEBUG_TOKEN) : CK.SP_DEBUG_TOKEN, "");
            } else if (!TextUtils.isEmpty(FileUtils.getDebugToken())) {
                putToken(FileUtils.getDebugToken());
                return FileUtils.getDebugToken();
            } else {
                return "";
            }
        } else {
            //正式环境
            if (!TextUtils.isEmpty(mSharedPreferences.getString(isEncodeKey ? encodeKey(CK.SP_TOKEN) : CK.SP_TOKEN, ""))) {
                return mSharedPreferences.getString(isEncodeKey ? encodeKey(CK.SP_TOKEN) : CK.SP_TOKEN, "");
            } else if (!TextUtils.isEmpty(FileUtils.getToken())) {
                putToken(FileUtils.getToken());
                return FileUtils.getToken();
            } else {
                return "";
            }
        }

    }

    public String getString(String key, String defaultValue) {
        checkKeyNull(key);
        return mSharedPreferences.getString(isEncodeKey ? encodeKey(key) : key, defaultValue);
    }

    public int getInt(String key, int defaultValue) {
        checkKeyNull(key);
        return mSharedPreferences.getInt(isEncodeKey ? encodeKey(key) : key, defaultValue);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        checkKeyNull(key);
        return mSharedPreferences.getBoolean(isEncodeKey ? encodeKey(key) : key, defaultValue);
    }

    public float getFloat(String key, float defaultValue) {
        checkKeyNull(key);
        return mSharedPreferences.getFloat(isEncodeKey ? encodeKey(key) : key, defaultValue);
    }

    public long getLong(String key, long defaultValue) {
        checkKeyNull(key);
        return mSharedPreferences.getLong(isEncodeKey ? encodeKey(key) : key, defaultValue);
    }

    public void remove(String key) {
        checkKeyNull(key);
        mSharedPreferences.edit().remove(isEncodeKey ? encodeKey(key) : key).apply();
    }

    public void clear() {
        mSharedPreferences.edit().clear().apply();
    }

    private String encodeKey(String key) {
        if (isEncodeKey)
            return Base64.encodeToString(key.getBytes(), Base64.NO_WRAP);
        else
            return key;
    }

    private String decodeKey(String key) {
        if (isEncodeKey)
            return new String(Base64.decode(key, Base64.NO_WRAP));
        else
            return key;
    }

    private void checkKeyNull(String key) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("Key is null");
        }
    }
}
